package com.kh.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.kh.tree.view.MyNodeViewFactory;

import me.texy.treeview.TreeNode;
import me.texy.treeview.TreeNodeValue;
import me.texy.treeview.TreeViewWrapper;

public class MainActivity extends AppCompatActivity {
    private TreeNode root;
    private TreeViewWrapper treeViewWrapper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FrameLayout fl_container = findViewById(android.R.id.content);
        buildTree();

        treeViewWrapper = new TreeViewWrapper(root, this, new MyNodeViewFactory());
        View view = treeViewWrapper.getView();
        view.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        fl_container.addView(view);
    }

    private void buildTree() {
        root = TreeNode.root();
        for (int i = 0; i < 20; i++) {
            TreeNodeValue treeNodeValue = new TreeNodeValue();
            treeNodeValue.text = "父--" + i;
            TreeNode treeNode = new TreeNode(0);
            treeNode.value = treeNodeValue;
            if (i != 3) { // avoids creating child nodes for "parent" 3 (which then is not a parent, so the semantic in the displayed text becomes incorrect)
                for (int j = 0; j < 10; j++) {
                    TreeNodeValue treeNodeChildValue = new TreeNodeValue();
                    treeNodeChildValue.text = "子--" + j;
                    TreeNode treeNode1 = new TreeNode(1);
                    treeNode1.value = treeNodeChildValue;
                    if (j != 5) { // avoids creating grand child nodes for child node 5
                        // For the child node without grand children there should not be any arrow displayed.
                        // In the demo code this can be handled in method 'SecondLevelNodeViewBinder.bindView' like this:
                        // imageView.setVisibility(treeNode.hasChild() ? View.VISIBLE : View.INVISIBLE);
                        for (int k = 0; k < 5; k++) {
                            TreeNodeValue treeNodeGrandChildValue = new TreeNodeValue();
                            treeNodeGrandChildValue.text = "孙--" + k;
                            TreeNode treeNode2 = new TreeNode(2);
                            treeNode2.value = treeNodeGrandChildValue;
                            treeNode1.addChild(treeNode2);
                        }
                    }
                    treeNode.addChild(treeNode1);
                }
            }
            root.addChild(treeNode);
        }
    }
}
